package com.cn.jdia.tas.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.cn.jdia.tas.context.SpringContextUtils;
import com.cn.jdia.tas.model.T_tasaccess;
import com.cn.jdia.tas.service.ITasAccessService;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

@Controller
@RequestMapping("/control")
public class CheckLoginController {

	@Resource
	private ITasAccessService accessService = null;

	@RequestMapping("/login")
	public String checkLogin(HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();

		String data = request.getParameter("data");

		Gson gson = new Gson();
		Map<String, String> map = gson.fromJson(data, new TypeToken<Map<String, String>>() {
		}.getType());

		String name = map.get("username");
		String pwd = map.get("password");

		System.out.println(name + " , " + pwd);

		// 查询人员信息
		T_tasaccess access = accessService.checklogin(name, pwd);

		String id = "NULL";
		String pname = "NULL";
		String dept = "NULL";
		String idCard = "NULL";

		// if (employee != null) {
		// id = employee.getEmployeeid();
		// pname = employee.getEmployeename();
		// dept = employee.getDepatment().getDeptname();
		// idCard = employee.getIdcardno();
		// }

		if (access != null) {
			id = access.getEmployeeid();
			pname = access.getEmployeename();
			dept = access.getDepatment().getDeptname();
			idCard = access.getIdcardno();
		}

		System.out.println("ID:" + id);
		System.out.println("DEPT:" + dept);
		System.out.println("CARD:" + idCard);

		Map<String, String> mmap = new HashMap<>();
		mmap.put("id", id);
		mmap.put("name", pname);
		mmap.put("dept", dept);
		mmap.put("card", idCard);

		String ret = gson.toJson(mmap);
		System.out.println(ret);

		out.write(ret);
		out.flush();
		out.close();

		return null;
	}

	@RequestMapping("/modifypwd")
	public String modifyPassword(HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();

		String data = request.getParameter("data");

		Gson gson = new Gson();
		Map<String, String> map = gson.fromJson(data, new TypeToken<Map<String, String>>() {
		}.getType());

		String id = map.get("id");
		String oldpwd = map.get("oldpwd");
		String newpwd = map.get("newpwd");

		System.out.println(id + " , " + oldpwd + " , " + newpwd);

		// ClassPathXmlApplicationContext w_context = new
		// ClassPathXmlApplicationContext("spring-mybatis.xml");
		// accessService = (ITasAccessService)
		// w_context.getBean("accessService");
		
		int ret = accessService.modifyPassword(id, oldpwd, newpwd);
		System.out.println(ret);
		String retstr = String.valueOf(ret);

		out.write(retstr);
		out.flush();
		out.close();

		return null;
	}

}
